package com.culturalCenter.dataCenter.Utils;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public final class DateFormatUtil {

    private static final ThreadLocal<DateFormat> SIMPLE_SECOND_FORMAT =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    private static final ThreadLocal<DateFormat> SIMPLE_DATE_FORMAT =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
    private static final ThreadLocal<DateFormat> IDCARD_DATE =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

    private static final ThreadLocal<DateFormat> SIMPLE_DATE_MONTH =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM"));

    private static final ThreadLocal<DateFormat> SIMPLE_YEAR =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy"));

    private static final ThreadLocal<DateFormat> SIMPLE_MONTH =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("MM"));

    private static final ThreadLocal<DateFormat> SIMPLE_DAY =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("dd"));

/* private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };*/

    private DateFormatUtil() {
    }

    public static Date parse(String target) {
        try {
            return SIMPLE_DATE_FORMAT.get().parse(target);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static Date parseIDCard(String birthday) {
        try {
            return IDCARD_DATE.get().parse(birthday);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static String formatSecond(Date target) {
        return SIMPLE_SECOND_FORMAT.get().format(target);
    }

    public static String format(Date target) {
        return SIMPLE_DATE_FORMAT.get().format(target);
    }

    // 格式化到月份
    public static String formatMonth(Date target) {
        return SIMPLE_DATE_MONTH.get().format(target);
    }

    // 获取月份
    public static String getMonth(Date date) {
        return SIMPLE_MONTH.get().format(date);
    }

    // 获取年份
    public static String getYear(Date date) {
        return SIMPLE_YEAR.get().format(date);
    }

    // 获取本月的最后一天
    public static String getLastDayByDate(Date date) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return SIMPLE_DAY.get().format(ca.getTime());
    }

    //通过当前时间获取本月开始时间和结束时间
    public static String  getOfMonth(LocalDateTime nowTime,String type){
        int currentMonth = nowTime.getMonthValue();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startOfMonth = LocalDateTime.of(nowTime.getYear(), currentMonth, 1, 0, 0, 0);
        if ("end".equals(type)){
            YearMonth nextMonth = YearMonth.from(startOfMonth).plusMonths(1);
            LocalDateTime endOfMonth = nextMonth.atDay(1).atStartOfDay().minusSeconds(1);
            return endOfMonth.format(formatter);
        }
        return startOfMonth.format(formatter);
    }

}
